﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using OnlineMealOrdering.Common;

namespace OnlineMealOrdering.Areas.WebSiteManage.Controllers
{
    public class UploadFilesController : Controller
    {

        public string UploadFiles(int type = 0)
        {
            Response.ContentType = "text/plain";
            Response.Charset = "utf-8";
            string fileGuid = null;
            string rawFileAbsolutePath = null;
            string rawFileRelativePath = null;
            string thumbAbsolutePath = null;
            string thumbRelativePath = null;
            string fileType = null;

            HttpPostedFileBase file = Request.Files["Filedata"];
            if (file != null)
            {
                var fileInfo = FileHelper.SaveImage(HttpContext.Server.MapPath("~/"), file.FileName, file.InputStream, true);
                rawFileAbsolutePath = fileInfo.RawFileAbsolutePath.Replace("\\", "/");
                rawFileRelativePath = fileInfo.RawFileAppRelativePath.Replace("\\", "/");
                thumbAbsolutePath = fileInfo.ThumbnailAbsolutePath.Replace("\\", "/");
                thumbRelativePath = fileInfo.ThumbnailAppRelativePath.Replace("\\", "/");
                fileGuid = fileInfo.FileId.ToString();
                fileType = fileInfo.FileType;

            }
            object obj = new { @fileGuid = fileGuid, @rawFileRelativePath = rawFileRelativePath, @thumbRelativePath = thumbRelativePath, fileType = fileType };
            return new JavaScriptSerializer().Serialize(obj);
        }

        public void CancelFiles(string filePath)
        {
            FileHelper.DeleteImages(HttpContext.Server.MapPath("~/"), filePath, "");
        }
    }
}
